﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.LDS
{
    static class Util
    {
       static System.Random random = new System.Random(unchecked((int)DateTime.Now.Ticks));

        /// <summary>
        /// generate a random number in [minV,maxV), between minV and maxV, not including maxV
        /// </summary>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public static int GenRandomInt(int minValue, int maxValue)
        {
            return random.Next(minValue, maxValue);
        }
        public static double GenRandomDouble(double minValue, double maxValue)
        {
            return minValue + (maxValue - minValue) * random.NextDouble();
        }

        public static void Swap<T>(ref T a, ref T b)
        {
            T tmp = a;
            a = b;
            b = tmp;
        }
        public static int getKeyForValueInMap( Dictionary<int, int> idMap, int value)
        {

          // internalMap is std::map
	      foreach (var it in idMap)
	      {
		    if (it.Value == value)
			return it.Key;   // return the first found key
	      }

	      return -1;
        }

public static List<T> CopyList<T>(List<T> t) 
        {
            List<T> tmp = new List<T>();
            foreach (T i in t)
            {
                tmp.Add((T)Activator.CreateInstance(typeof(T), i));
            }
            return tmp;
        }
        

        //BugFree
        public static List<string> PartitionString(string s, string separator)
        {
            List<string> result = new List<string>();
            string curEntry = "";
            for (int outerCharacterIndex = 0; outerCharacterIndex < s.Length; outerCharacterIndex++)
            {
                bool isSeperator = true;
                for (int innerCharacterIndex = 0; innerCharacterIndex < separator.Length && outerCharacterIndex + innerCharacterIndex < s.Length && isSeperator; innerCharacterIndex++)
                {
                    if (s[outerCharacterIndex + innerCharacterIndex] != separator[innerCharacterIndex])
                    {
                        isSeperator = false;
                    }
                }
                if (isSeperator)
                {
                    if (curEntry.Length > 0)
                    {
                        result.Add(curEntry);
                        curEntry = "";
                    }
                    outerCharacterIndex += separator.Length - 1;
                }
                else
                {
                    curEntry += (s[outerCharacterIndex]);
                }
            }
            if (curEntry.Length > 0)
            {
                result.Add(curEntry);
            }
            return result;
        }

        //BugFree
        public static List<int> StringToIntegerList(string s, string prefix, string separator = " ")
        {
            string subString;
            if (prefix == "")
            {
                subString = s;
            }
            else
            {
                subString = PartitionString(s, prefix)[0];
            }

            List<string> parts = PartitionString(subString, separator);

            List<int> result = new List<int>();

            for (int resultIndex = 0; resultIndex < parts.Count; resultIndex++)
            {
                if (parts[resultIndex] == "nan" || parts[resultIndex] == "inf")
                {
                    result.Add(0);
                    continue;
                }
                try
                {
                    result.Add(int.Parse(parts[resultIndex]));
                    
                }
                catch
                {
                    try
                    {
                        result.Add((int)float.Parse(parts[resultIndex]));
                    }
                    catch
                    {
                        throw new FormatException(string.Format("StringToIntegerList ERROR, input is \"{0}\"", parts[resultIndex]));
                    }
                }
            }
            return result;
        }

        public static List<float> StringToFloatList(string s, string prefix, string separator = " ")
        {
            string subString;
            if (prefix == "")
            {
                subString = s;
            }
            else
            {
                subString = PartitionString(s, prefix)[0];
            }

            List<string> parts = PartitionString(subString, separator);

            List<float> result = new List<float>();

            for (int resultIndex = 0; resultIndex < parts.Count; resultIndex++)
            {
                if(parts[resultIndex]=="nan" || parts[resultIndex] == "inf")
                {
                    result.Add(0.0f);
                    continue;
                }
                try
                {
                    result.Add(float.Parse(parts[resultIndex]));
                }
                catch
                {
                    throw new FormatException(String.Format("StringToFloatList ERROR, input is \"{0}\"", parts[resultIndex]));
                }
            }
            return result;
        }


        public static Vector3 TransformVector(Matrix4x4 transMat, Vector3 v)
        {
            Vector3 transV = new Vector3(transMat[0] * v.x + transMat[1] * v.y + transMat[2] * v.z,
            transMat[4] * v.x + transMat[5] * v.y + transMat[6] * v.z,
            transMat[8] * v.x + transMat[9] * v.y + transMat[10] * v.z);
            return transV;
        }

        public static int StringToInt(string s)
        {
            int result;
            try
            {
                result = int.Parse(s);
            }
            catch
            {
                throw new FormatException(String.Format("StringToInt ERROR, input is \"{0}\"", s));
            }
            return result;
        }



        public static String GetIntString(List<int> intVector, String separator)
        {
            if (intVector.Count == 0)
            {
                String s = "";
                return s;
            }

            String outString = "";
            for (int i = 0; i < intVector.Count() - 1; i++)
            {
                outString += (intVector[i] + separator);
            }

            outString += intVector[intVector.Count - 1];

            return outString;
        }


    }
}
